package com.cinema.service;

import com.cinema.model.*;
import com.cinema.util.JsonFileStorage;
import java.util.*;
import java.time.LocalDateTime;

public class TicketService {
    private Map<String, Ticket> tickets;
    private static final String TICKETS_FILE = "data/tickets.json";

    public TicketService() {
        loadTickets();
    }

    private void loadTickets() {
        try {
            tickets = JsonFileStorage.loadFromFile(HashMap.class, TICKETS_FILE);
            if (tickets == null) {
                tickets = new HashMap<>();
            }
        } catch (Exception e) {
            System.out.println("加载票务数据失败: " + e.getMessage());
            tickets = new HashMap<>();
        }
    }

    private boolean saveTickets() {
        try {
            JsonFileStorage.saveToFile(tickets, TICKETS_FILE);
            return true;
        } catch (Exception e) {
            System.out.println("保存票务数据失败: " + e.getMessage());
            return false;
        }
    }

    public Ticket purchaseTicket(Screening screening, String seatNumber, String purchaser) {
        if (!screening.getHall().occupySeat(seatNumber)) {
            System.out.println("座位已被占用或不存在: " + seatNumber);
            return null;
        }

        if (!screening.sellTicket()) {
            System.out.println("售票失败");
            return null;
        }

        Ticket ticket = new Ticket(screening, seatNumber, purchaser);
        tickets.put(ticket.getTicketId(), ticket);

        if (saveTickets()) {
            return ticket;
        }
        return null;
    }

    public boolean refundTicket(String ticketId) {
        Ticket ticket = tickets.get(ticketId);
        if (ticket == null) {
            System.out.println("票号不存在");
            return false;
        }

        if (!ticket.canRefund()) {
            System.out.println("已超过退票时间");
            return false;
        }

        if (ticket.getStatus() != TicketStatus.VALID) {
            System.out.println("票已使用或已退款");
            return false;
        }

        // 释放座位
        ticket.getScreening().getHall().releaseSeat(ticket.getSeatNumber());
        ticket.getScreening().refundTicket();
        ticket.setStatus(TicketStatus.REFUNDED);

        return saveTickets();
    }

    public Ticket getTicketById(String ticketId) {
        return tickets.get(ticketId);
    }

    public List<Ticket> getTicketsByPurchaser(String purchaser) {
        List<Ticket> result = new ArrayList<>();
        for (Ticket ticket : tickets.values()) {
            if (ticket.getPurchaser().equals(purchaser) &&
                    ticket.getStatus() == TicketStatus.VALID) {
                result.add(ticket);
            }
        }
        return result;
    }

    public double getDailyRevenue(LocalDateTime date) {
        double revenue = 0;
        for (Ticket ticket : tickets.values()) {
            if (ticket.getPurchaseTime().toLocalDate().equals(date.toLocalDate()) &&
                    ticket.getStatus() == TicketStatus.VALID) {
                revenue += ticket.getFinalPrice();
            }
        }
        return revenue;
    }
}